﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Use this to set the color balance
    /// </summary>
    public class ColorBalance : BaseImageFilter
    {
        protected double m_dValue = 0.0f;

        /// <summary>
        /// Use color balance with a default value of 10
        /// </summary>
        public ColorBalance()
            : this(10)
        {
        }

        /// <summary>
        /// Initialize with given value
        /// </summary>
        /// <param name="_nValue"></param>
        public ColorBalance(int _nValue)
        {
            m_dValue = _nValue;
        }

        /// <summary>
        /// This is used by clone to create a deep copy
        /// </summary>
        /// <param name="_colorBalance"></param>
        internal ColorBalance(ColorBalance _colorBalance)
        {
            this.m_dValue = _colorBalance.m_dValue;
        }

        /// <summary>
        /// Gets or sets the intensity
        /// </summary>
        public double Intensity
        {
            get { return m_dValue; }
            set { m_dValue = value; }
        }

        /// <summary>
        /// Execute the filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            double vR = m_dValue / 100.0;
            double vG = m_dValue / 100.0;
            double vB = m_dValue / 100.0;
            double fR = 0.0f, fG = 0.0f, fB = 0.0f;
            if (vR < 0)
            {
                fR = 1 + vR;
            }
            else
            {
                fR = vR;
            }
            if (vG < 0)
            {
                fG = 1 + vG;
            }
            else
            {
                fG = vG;
            }
            if (vB < 0)
            {
                fB = 1 + vB;
            }
            else
            {
                fB = vB;
            }
            int width = bitmap.Width;
            int height = bitmap.Height;
            Color color;
            double dR = 0.0f, dG = 0.0f, dB = 0.0f;

            int x = 0;
            int y = 0;

            for (x = 0; x < width; x++)
            {
                for (y = 0; y < height; y++)
                {
                    color = bitmap.GetPixel(x, y);
                    if (vR < 0)
                    {
                        dR = color.R * fR;
                    }
                    else
                    {
                        dR = color.R + (255 - color.R) * fR;
                    }
                    if (vG < 0)
                    {
                        dG = color.G * fG;
                    }
                    else
                    {
                        dG = color.G + (255 - color.G) * fG;
                    }
                    if (vB < 0)
                    {
                        dB = color.B * fB;
                    }
                    else
                    {
                        dB = color.B + (255 - color.B) * fB;
                    }
                    bitmap.SetPixel(x, y, Color.FromArgb((int)dR, (int)dG, (int)dB));
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Creates a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new ColorBalance(this);
        }
    }
}
